<?php


/**
 * 
 * 获取发布事件 详情1
 * type SH求助 CH救护
*/
function get_affair_info($user_id,$log_id,$type){
	$info=array();
	  
	$where = '';
	$rescue_status = array();
	$user_sex=array('未知','男','女');
	$usericon = GZ_Api::get_url('/Mapi/public/images/usericon.png');

	if($type=='SH'){
		$where = ' AND r.user_id = '.$user_id;
		$rescue_status=array('待救援','救援中','完成救援','已评价','取消');
  	}
	if($type=='CH'){
		$where = ' AND r.rescue_id = '.$user_id;
    	$rescue_status=array('救援他','救援中','完成救援','已评价','取消');
  	}

	$info = array();
	$sql = "SELECT u.user_name,u.user_phone,u.user_integral,u.height,u.weight,u.age,u.headimg,u.sex".
	",r.id,r.rescue_id,r.reality_money,r.user_describe,r.status,r.incident_desc,r.user_coordinate,rescue_coordinate FROM " .
	$GLOBALS['db']->table('rescue_log')." AS r LEFT JOIN ".$GLOBALS['db']->table('users').
	" AS u on r.user_id = u.user_id WHERE r.id='$log_id'".$where;
	$res=$GLOBALS['db']->getRow($sql);
	
	
    if($res)
	{
		$height=!empty($res['height'])?$res['height']:'0.00';
        $weight=!empty($res['weight'])?$res['weight']:'0.00';
		
		//发布人及事件基本信息
		$info['log_id']				 = $res['id'];
		$info['demander_headimg']	 = '';//头像
		$info['demander_name']		 = '';//姓名
		$info['demander_integral']	 = ''; 
		$info['demander_style']		 = '';
		$info['reality_money']		 = $res['reality_money'];
		$info['rescue_status']		 = $rescue_status[$res['status']];//救援状态
		$info['status']				 = $res['status'];
		$info['incident_desc']		 = !empty($res['incident_desc'])?$res['incident_desc']:'';//现场描述
		
		//发布人坐标
		$info['demander_coordinate'] = array('lat'=>'','lng'=>'');;
		if(!empty($res['user_coordinate'])){ 
			$info['demander_coordinate'] = json_decode($res['user_coordinate']);
		}
		//救援人坐标
		$info['rescue_coordinate'] = array('lat'=>'','lng'=>'');	
		if($res['status']>0){ //已经有人救援（ 0未救援） 
			if(!empty($res['rescue_coordinate'])){ 
				$info['rescue_coordinate'] = json_decode($res['rescue_coordinate']);
			}
		}

		//获取发布人信息
		$demander_user = array('user_name' =>'','user_phone'=>'','user_integral'=>'','user_headimg'=>'','user_style'=>'','user_coordinate'=>'');
		$demander_user['user_name']	 = $res['user_name'];//姓名
		$demander_user['user_phone'] = $res['user_phone'];//发布人电话
		$demander_user['user_headimg']	 = empty($res['headimg'])?$usericon:GZ_Api::get_url($res['headimg']);//头像
		$demander_user['user_integral']	 = $res['user_integral']; 
		$demander_user['user_style'] = $res['age'].'岁  '.$user_sex[$res['sex']].'  '.$height.'cm  '. $weight.'斤';
		$demander_user['user_coordinate'] = $info['demander_coordinate'];
		$info['demander_user'] = $demander_user;

		//获取救援人信息
		$rescue_user = array('user_name' =>'','user_phone'=>'','user_integral'=>'','user_headimg'=>'','user_coordinate'=>array('lat'=>'','lng'=>''));
		if($res['status']==1 && $res['rescue_id']>0){

			$sql = "select user_name,user_phone,user_integral,headimg from ".$GLOBALS['db']->table('users')." where user_id=".$res['rescue_id'];
			$res_user = $GLOBALS['db']->getRow($sql);

  			$rescue_user['user_name'] = empty($res_user['user_name'])?'爱心用户':$res_user['user_name'];
  			$rescue_user['user_phone'] = $res_user['user_phone'];
  			$rescue_user['user_integral'] = $res_user['user_integral'];
  			$rescue_user['user_headimg'] = empty($res_user['headimg'])?$usericon:GZ_Api::get_url($res_user['headimg']);//头像
  			$rescue_user['user_coordinate'] = $info['rescue_coordinate'];
		}
		
		$info['rescue_user'] = $rescue_user;
		  
		return $info;
		
    }else{
		return false;
	}
  
}


/**
 * 获取发布事件 详情2
 * type SH求助 CH救护
*/
function get_detail_info($user_id,$log_id,$type){
	
	$info=array();
	$where = '';
	$rescue_status = array();
  	$user_sex=array('未知','男','女');
	
  	if($type=='SH'){
		$where = ' AND a.user_id = '.$user_id;
		$rescue_status=array('待救援','救援中','完成救援','已评价','取消');
  	}
	if($type=='CH'){
		$where = ' AND a.rescue_id = '.$user_id;
    	$rescue_status=array('救援他','救援中','完成救援','已评价','取消');
  	}
  
	$sql = "SELECT a.*,b.headimg,b.user_name,b.user_integral,b.sex,b.height,b.weight,b.label,".
	"b.user_phone,c.user_id as ruser_id,c.user_name as rescue_name,c.headimg as rescue_headimg FROM " .
	$GLOBALS['db']->table('rescue_log'). " AS a LEFT JOIN ".$GLOBALS['db']->table('users').
	" AS b on a.user_id = b.user_id". " LEFT JOIN ".$GLOBALS['db']->table('users').
	 " AS c on a.rescue_id=c.user_id WHERE a.id='$log_id'".$where;
	$res=$GLOBALS['db']->getRow($sql);
	
	if($res){
		
		$info['log_id']=$res['id'];
		$info['demander_name']=$res['user_name'];
		$info['demander_headimg']=GZ_Api::get_url($res['headimg']);
		$info['demander_integral']=$res['user_integral'];
		//标签
		$info['demander_label']=array();
		if(!empty($res['label'])){
			$info['demander_label']=json_decode($res['label']);
		}
		
		if($type=='SH'){
			$info['award_money']=$res['award_money'];//奖励金额
		}
		if($type=='CH'){
			$info['award_money']=$res['reality_money'];//实付金额
		}
		//定位地址
		$info['address']=isset($res['address'])?$res['address']:'';
		
		//用户特征
		$height=isset($res['height'])?$res['height']:'0.00';
	 	$weight=isset($res['weight'])?$res['weight']:'0.00';
	 	$info['demander_style']=$user_sex[$res['sex']].'  '.$height.'cm  '. $weight.'斤';   
		//救援信息
		if($res['status']>0 ){ //已经有人救援（ 0未救援） 
			 $info['rescue_info']=array(
			// 'rescue_id'=>isset($res['ruser_id'])?$res['ruser_id']:'',
			 'rescue_name'=>isset($res['rescue_name'])?$res['rescue_name']:'',
			 'rescue_headimg'=>GZ_Api::get_url($res['rescue_headimg']),
			 );
		}else{
			 $info['rescue_info']=array(
			 'rescue_name'=>'',
			 'rescue_headimg'=>'',
			 );
		}
		
		$info['demander_phone']=isset($res['user_phone'])?$res['user_phone']:'';
		//情况描述
		$info['incident_desc']=isset($res['incident_desc'])?$res['incident_desc']:'';
		//现场图片
		$info['user_imglist'] = '';
		if(!empty($res['user_imglist'])){
			$imglist = json_decode($res['user_imglist']);

			if(is_array($imglist)){
				foreach($imglist as $k=>$v){
					$info['user_imglist'][$k]=GZ_Api::get_url($v);
				}

			}
		}
 
		$info['rescue_type'] =get_rescue_typeName($res['rescue_type']);//发布类型
		$info['add_time'] = date('Y-m-d h:i:s',$res['add_time']);
		$info['status'] = $res['status'];
		$info['rescue_status'] = $rescue_status[$res['status']];
		  
	}
	
	return $info;
  
}


//救护类型名称
function get_rescue_typeName($id){
  $result=$GLOBALS['db']->getOne("select name from ".$GLOBALS['db']->table('rescue_type')." where id='$id'");
  return $result;
}
?>